package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TMStatMfdMchntMapper;
import cc.rengu.igas.momp.common.entity.TMStatMfdMchnt;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 手续费减免活动商户统计表数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/20 22:30
 */

public class TMStatMfdMchntMapperImpl implements TMStatMfdMchntMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 使用主键查询手续费减免活动商户统计表数据数据库操作接口实现
     *
     * @param instId          法人机构号
     * @param mcNo            营销活动编号
     * @param ruleIndex       规则顺序号
     * @param mchntNo         商户编号
     * @param periodStartDate 统计周期起始日期
     * @param periodEndDate   统计周期结束日期
     * @return cc.rengu.igas.momp.common.entity.TMStatMfdMchnt 手续费减免活动商户统计表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/20 23:55
     */
    @Override
    public TMStatMfdMchnt selectMchntFeeDiscountMchntStatisticsByPK(String instId, String mcNo, String ruleIndex, String mchntNo, String periodStartDate, String periodEndDate) throws Exception {

        TMStatMfdMchnt tmStatMfdMchnt = new TMStatMfdMchnt();
        tmStatMfdMchnt.setInstId(instId);
        tmStatMfdMchnt.setMcNo(mcNo);
        tmStatMfdMchnt.setRuleIndex(ruleIndex);
        tmStatMfdMchnt.setMchntNo(mchntNo);
        tmStatMfdMchnt.setStatStartDate(periodStartDate);
        tmStatMfdMchnt.setStatEndDate(periodEndDate);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeDiscountMchntStatisticsByPK");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdMchnt, tmStatMfdMchnt);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>的商户<{}>的商户手续费优惠活动<{}>规则编号<{}>在统计周期<{}>-<{}>的统计数据失败!<{}>",
                    instId, mchntNo, mcNo, ruleIndex, periodStartDate, periodEndDate, iReturnCode);
            return null;
        }

        return tmStatMfdMchnt;
    }

    /**
     * @param instId
     * @param mchntNo
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMStatMfdMchnt>
     * @throws Exception 异常信息
     * @author JL PANG
     * @date 2020/9/16
     * @description 手续费减免活动商户统计表数据查询接口实现
     */
    @Override
    public List<TMStatMfdMchnt> selectMFDiscountMchntStatisticsByinstIdMcNoMchntNo(String instId, String mcNo, String mchntNo, String nowtDate) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MCHNT_NO=[" + mchntNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMFDiscountMchntStatisticsByinstIdMcNoMchntNo");

        // 按接口赋查询条件
        TMStatMfdMchnt tmStatMfdMchnt = new TMStatMfdMchnt();
        tmStatMfdMchnt.setInstId(instId);
        tmStatMfdMchnt.setMchntNo(mchntNo);
        tmStatMfdMchnt.setMcNo(mcNo);
        tmStatMfdMchnt.setStatStartDate(nowtDate);
        tmStatMfdMchnt.setStatEndDate(nowtDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmStatMfdMchnt, TMStatMfdMchnt.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取营销活动商户手续费统计表数据失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMStatMfdMchnt) (item)).collect(Collectors.toList());
    }

    /**
     * 更新手续费营销活动商户统计表T_M_STAT_MFD_MCHNT中的统计结束日期stat_end_date
     *
     * @param tmStatMfdMchnt
     * @throws Exception 异常信息
     * @author JL Pang
     * @date 2020/9/16
     */
    @Override
    public int updateMfdEndDate(TMStatMfdMchnt tmStatMfdMchnt) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMfdEndDate");
        tmStatMfdMchnt.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdMchnt, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新手续费营销活动商户统计表T_M_STAT_MFD_MCHNT失败！");
        }
        return iReturnCode;
    }

    /**
     * 查询当前日期所在的营销活动商户统计数据
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex 规则顺序号
     * @param mchntNo   商户编号
     * @return cc.rengu.igas.momp.common.entity.TMStatMfdMchnt 手续费减免活动商户统计表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/9/15 23:31
     */
    @Override
    public TMStatMfdMchnt selectNowPeriodDateTMStatMfdMchnt(String instId, String mcNo, String ruleIndex, String mchntNo, String txnDate) throws Exception {

        TMStatMfdMchnt tmStatMfdMchnt = new TMStatMfdMchnt();
        tmStatMfdMchnt.setInstId(instId);
        tmStatMfdMchnt.setMcNo(mcNo);
        tmStatMfdMchnt.setRuleIndex(ruleIndex);
        tmStatMfdMchnt.setMchntNo(mchntNo);
        tmStatMfdMchnt.setStatStartDate(txnDate);
        tmStatMfdMchnt.setStatEndDate(txnDate);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectNowPeriodDateTMStatMfdMchnt");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdMchnt, tmStatMfdMchnt);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}> 商户<{}> 手续费优惠活动<{}> 规则编号<{}> 查询当前所在的统计周期的统计数据失败!<{}>",
                    instId, mchntNo, mcNo, ruleIndex, iReturnCode);
            return null;
        }

        return tmStatMfdMchnt;
    }

    /**
     * 插入新增商户数据数据库操作接口实现
     *
     * @param tmStatMfdMchntList 营销活动商户统计表数据
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @since 2020-07-07 23:00
     */
    @Override
    public int insertNewStatMfdMchntData(List<TMStatMfdMchnt> tmStatMfdMchntList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertNewStatMfdMchntData");

        for (TMStatMfdMchnt tmStatMfdMchnt : tmStatMfdMchntList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdMchnt, TMStatMfdMchnt.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>的新入驻商户<{}>的减免达标统计信息失败!<{}>", tmStatMfdMchnt.getInstId(), tmStatMfdMchnt.getMcNo(), tmStatMfdMchnt.getMchntNo(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 使用主键查询手续费减免活动商户统计表累计优惠金额数据库操作接口实现
     *
     * @param instId  法人机构号
     * @param mchntNo 商户编号
     * @return cc.rengu.igas.momp.common.entity.TMStatMfdMchnt 手续费减免活动商户统计表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/5/28 14:55
     */
    @Override
    public TMStatMfdMchnt selectMchntFeeTotalAmtMchntStatistics(String instId, String mchntNo) throws Exception {

        TMStatMfdMchnt tmStatMfdMchnt = new TMStatMfdMchnt();
        tmStatMfdMchnt.setInstId(instId);
        tmStatMfdMchnt.setMchntNo(mchntNo);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeTotalAmtMchntStatistics");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdMchnt, tmStatMfdMchnt);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>的商户<{}>的商户手续费优惠活动累计优惠金额数据失败!<{}>", instId, mchntNo, iReturnCode);
            return null;
        }

        return tmStatMfdMchnt;
    }

}
